package com.czkt.springboot_mybatis_test.controller;


import com.czkt.springboot_mybatis_test.entity.BasDict;
import com.czkt.springboot_mybatis_test.entity.Page;
import com.czkt.springboot_mybatis_test.entity.Product;
import com.czkt.springboot_mybatis_test.service.IBasDictService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2024-06-18
 */
@Controller
@RequestMapping("/springboot_mybatis_test/bas-dict")
public class BasDictController {
    @Resource
    IBasDictService basDictService;

    @RequestMapping("/crm/dict/list")
    public String dictListShow(String dictId, String dictItem, Integer pageIndex, Model model){
        if(pageIndex == null){
            pageIndex = 1;
        }
        Page page = new Page();

        if(dictId == null){
            dictId = "";
        }
        if(dictItem == null){
            dictItem = "";
        }
        Integer count = basDictService.selectBasDictCount(dictId, dictItem);
        if(count == 0){
            count = 1;
        }
        page.setTotalCount(count);
        if(pageIndex < 1){
            pageIndex = 1;
        }
        if(pageIndex > page.getTotalPageCount()){
            pageIndex = page.getTotalPageCount();
        }
        int index = (pageIndex - 1) * page.getPageSize();
        page.setPageNo(pageIndex);

        List<BasDict> products = basDictService.selectBasDictList(dictId,dictItem,index,page.getPageSize());
        page.setBasDictList(products);
        model.addAttribute("dictPage",page);
        model.addAttribute("dictId",dictId);
        model.addAttribute("dictItem",dictItem);
        return "/dict/list";
    }

    @RequestMapping("/dict/edit")
    public String updateBasDict(String dictId,Model model){
        model.addAttribute("dict",basDictService.selectBasDict(dictId));
        return "/dict/edit";
    }

    @RequestMapping("/dict/save")
    public String toUpdateBasDict(BasDict basDict){
        basDictService.toUpdateBasDict(basDict);
        return "redirect:/springboot_mybatis_test/bas-dict/crm/dict/list";
    }

    @RequestMapping("/crm/dict/del/{dictId}")
    @ResponseBody
    public Object deleteDictId(@PathVariable("dictId") String dictId){
        Map<String,Object> resultMap = new HashMap<>();
        basDictService.deleteBasDict(dictId);
        resultMap.put("delResult","true");
        return resultMap;
    }

    @RequestMapping("/dict/add")
    public String insertBasDict(){
        return "/dict/add";
    }

    @RequestMapping("/crm/dict/save")
    public String toInsertBasDict(BasDict basDict){
        basDictService.toInsertBasDict(basDict);
        return "redirect:/springboot_mybatis_test/bas-dict/crm/dict/list";
    }
}
